package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAppUser;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2023 - 根据卡号查询用户姓名
 *
 * @Author cwledit
 * @DATE 2017/6/23.
 */
@Service("upp2023")
public class Upp2023 extends BaseService implements IService {

    private String SERVER_NAME = "upp.card.list.query";


    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap =  request.getData();
        String card_no = paramsStringFilter(paramMap.get("card_no"));
        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null){
            return renderFAIL(MsgCode.Code_1007,response,header);
        }
        if (StringUtils.isEmpty(card_no)){
            return renderFAIL(MsgCode.Code_1009,"卡号不能为空!",response,header);
        }
        Map data = new HashMap();
        data.put("card_no",card_no);
        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject.get("name"),response,header);
        }else{
            String msg = jsonObject.getString("msg");
            String errMsg = "";
            if (msg.equals("[温州市民卡运营平台]提醒您:市民卡卡号验证失败")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:商户或网点未授权操作当前卡类型")){
                errMsg = "该市民卡状态异常，无法进行充值！";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:参数值不正确")){
                errMsg = "输入有误,请核对";
            }else
            if (msg.equals("[温州市民卡运营平台]提醒您:短信验证码错误")){
                errMsg = "短信验证码错误";
            }else
            if (msg.equals("市民卡网关返回异常:服务密码不等于6位")){
                errMsg = "服务密码不正确";
            }else{
                errMsg = msg;
            }
            return renderFAIL(MsgCode.Code_1009,errMsg,response,header);
        }
    }

}
